//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    region: [],
    customItem: '全部',
    status: 0,
    location: '',
    address: '',
    id: 0,
    unit:'',
    store_id: 0,
    community_name: '',
    units:[],
  },
  bindPickerChange: function (e) {
    var v = Number(e.detail.value) ;
    this.data.building_key = this.data.buildings[v].key;
    this.setData({ unit: this.data.units[v]});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.id = options.id;
      this.init();
    }
    var that = this;
    app.getUserInfo(function(userInfo){
      that.data.userInfo = userInfo;
      that.getunits();
    })
    
  },

  getunits:function(){
    var that = this;
    app.api("api/community_rooms/getBuilding", {
      community_id: this.data.userInfo.community.community_id
    },function(res){
       if(res.code!=200){
         app.alert(res.msg);
         return ;
       }
       var units =[];
      console.log(res.data);
       for (var i in res.data){
         var key = res.data[i]['key'];
         units.push(res.data[i]['group']+res.data[i]['building']+res.data[i]['unit']) ;
       }
       that.data.buildings = res.data;
       that.setData({ units: units});
    })
  },

  // onShow: function () {
  //   var id = app.getPageData("index/issue", "id");
  //   if (id) {
  //     this.data.id = id;
  //     this.init();
  //     app.setPageData("index/issue", { id: 0 });
  //   }
  // },
  init: function () {
    var that = this;
    app.api("api/community_rooms/find", {
      id: this.data.id,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      //var poi = res.data.community_locations.split("|")
      that.setData({
        data: res.data.data,
      });
    })
  },

  formSubmit: function (e) {
    var postData = e.detail.value;
    if (postData.out_room_id == "") {
      app.alert("请输小区房屋唯一编号");
      return false;
    }
    if (this.data.building_key == "") {
      app.alert("请选择小区单元");
      return false;
    }
    // if (postData.building == "") {
    //   app.alert("请输入楼栋名称");
    //   return false;
    // }
    // if (postData.units == "") {
    //   app.alert("请输入单元名称");
    //   return false;
    // }

    if (postData.room == "") {
      app.alert("请输入房号");
      return false;
    }

    var that = this;
    that.setData({ 'loading': 1 });
    that.setData({ 'disabled': 1 });
    app.api("api/community_rooms/add", {
      method: 'POST',
      out_room_id: this.data.id ? this.data.id : postData.out_room_id,
      group: postData.group,
      building_key: this.data.building_key,
      building: postData.building,
      unit: postData.units,
      room: postData.room,
      address: postData.address,
      //out_room_id: postData.out_room_id,
      
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
     // that.reset();
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000
      })
      app.setPageData("tenement/index", { reload: 1 });
      wx.navigateBack();
    })

  }


})
